home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / ACL / Examples / ACL-World / Sources / Main.cp < prev    next >
Encoding:
Text File  |  1994-10-01  |  8.8 KB  |  478 lines  |  [TEXT/MPCC]

  1.  
  2. /********************************************
  3.  **** ACL-World
  4.  ****
  5.  **** main.cp
  6.  ****
  7.  **** Created:      21 August 1994
  8.  **** Modified:     29 August 1994
  9.  **** Version:      0
  10.  **** Compatible:   C++, Mac System 7
  11.  ****
  12.  **** Description:    Main part of the demo
  13.  ****
  14.  *******************/
  15.  
  16. #include "ACL-World.h"
  17.  
  18.  
  19. //****************************************
  20.  
  21.  
  22. static const short PICT_PERS         = 130;
  23. static const short PICT_SHOCKPERS     = 139;
  24. static const short PICT_FLYPERS     = 144;
  25.  
  26. static const short PICT_BACKGROUND     = 138;
  27.  
  28. static const short PICT_A             = 135;
  29. static const short PICT_C             = 136;
  30. static const short PICT_L             = 137;
  31.  
  32.  
  33. AnimFrameDef animpers[] =             {{PICT_PERS+2,NULL,2,0,0,afcmd_frame},
  34.                                     {PICT_PERS+3,NULL,2,0,0,afcmd_frame},
  35.                                     {PICT_PERS+4,NULL,2,0,0,afcmd_frame},
  36.                                     {PICT_PERS+3,NULL,2,0,0,afcmd_frame},
  37.                                     {PICT_PERS+2,NULL,2,0,0,afcmd_frame},
  38.                                     {PICT_PERS+0,NULL,2,0,0,afcmd_frame},
  39.                                     {PICT_PERS+1,NULL,2,0,0,afcmd_endsequence},
  40.                                     {PICT_SHOCKPERS+0,NULL,1,0,0,afcmd_frame},
  41.                                     {PICT_SHOCKPERS+1,NULL,1,0,0,afcmd_frame},
  42.                                     {PICT_SHOCKPERS+2,NULL,1,0,0,afcmd_endsequence},        
  43.                                     {PICT_FLYPERS+0,NULL,3,0,0,afcmd_frame},
  44.                                     {PICT_FLYPERS+1,NULL,3,0,0,afcmd_frame},
  45.                                     {PICT_FLYPERS+2,NULL,3,0,0,afcmd_endanim}};        
  46.  
  47.  
  48.  
  49. AnimControl    perscontrols[2];
  50. AnimControl    a_controls[4];
  51. AnimControl    l_controls[4];
  52.  
  53.  
  54.  
  55. //****************************************
  56.  
  57. void ACLWorld::inittoolbox()
  58. {
  59.     InitGraf((Ptr) &qd.thePort);
  60.     InitFonts();
  61.     InitWindows();
  62.     InitMenus();
  63.     FlushEvents(everyEvent,0);
  64.     TEInit();
  65.     InitDialogs(0L);
  66.     InitCursor();
  67.  
  68.     GetDateTime((unsigned long*)&qd.randSeed);    // Initializes the random number generator
  69. }
  70.  
  71. //****************************************
  72.  
  73.  
  74. ACLWorld::ACLWorld()
  75. {
  76.     long l;
  77.  
  78.     inittoolbox();
  79.     initmenubar();
  80.     introwarning();
  81.     
  82. }
  83.  
  84. //****************************************
  85.  
  86. ACLWorld::~ACLWorld()
  87. {
  88.     freemenubar();
  89. }
  90.  
  91. //****************************************
  92.  
  93.  
  94. void ACLWorld::run(void)
  95. {
  96.     Boolean        done = FALSE;
  97.     EventRecord    theEvent;
  98.     WindowPtr    whichWindow;
  99.     DialogPtr    whichDlog;
  100.     short        itemHit;
  101.     short        part;
  102.  
  103.     initmenu();
  104.  
  105.     
  106.     while(!done)
  107.     {
  108.         animbase->update();
  109.  
  110.         if (WaitNextEvent(everyEvent,&theEvent,0,NULL))
  111.         {
  112.             if(DialogSelect(&theEvent,&whichDlog,&itemHit)) 
  113.             {
  114.                 switch(itemHit)
  115.                 {
  116.                     case 2:            // intro demo
  117.                     closemenu();
  118.                     if (do_intro()) return;
  119.                     initmenu();
  120.                     break;
  121.                     
  122.                     case 3:            // Controls demo
  123.                     closemenu();
  124.                     if (do_controls()) return;
  125.                     initmenu();
  126.                     break;
  127.  
  128.                     case 4:            // Composed anim demo
  129.                     closemenu();
  130.                     if (do_companim()) return;
  131.                     initmenu();
  132.                     break;
  133.  
  134.                     case 5:            // Sorting demo
  135.                     closemenu();
  136.                     if (do_sorting()) return;
  137.                     initmenu();
  138.                     break;
  139.  
  140.                     case 6:            // Collisions demo
  141.                     closemenu();
  142.                     if (do_collisions()) return;
  143.                     initmenu();
  144.                     break;
  145.  
  146.                     case 7:            // Animmask demo
  147.                     closemenu();
  148.                     if (do_animmask()) return;
  149.                     initmenu();
  150.                     break;
  151.  
  152.                     case 8:            // Scrolling demo
  153.                     closemenu();
  154.                     if (do_scrolling()) return;
  155.                     initmenu();
  156.                     break;
  157.                     
  158.                     
  159.                 }
  160.             }        
  161.         
  162.             switch(theEvent.what)
  163.             {
  164.                 case updateEvt:
  165.                 whichWindow = (WindowPtr)theEvent.message;
  166.                 if (whichWindow==dialog)
  167.                 {
  168.                     BeginUpdate(whichWindow);
  169.                     animbase->updatewindow();
  170.                     EndUpdate(whichWindow);
  171.                 }
  172.                 break;
  173.                 
  174.                 case keyDown:
  175.                 done = docmdkey(&theEvent);
  176.                 break;
  177.                 
  178.  
  179.                 case mouseDown:
  180.                 part = FindWindow(theEvent.where,&whichWindow);
  181.                 if (whichWindow==dialog)
  182.                 {
  183.                     switch(part)
  184.                     {
  185.                         case inGoAway:
  186.                         done = TrackGoAway(whichWindow,theEvent.where);
  187.                         break;
  188.                         
  189.                         case inDrag:
  190.                         DragWindow(whichWindow,theEvent.where,&qd.screenBits.bounds);
  191.                         break;
  192.                     }
  193.                 }
  194.                 else
  195.                 {
  196.                     switch(part)
  197.                     {
  198.                         case inMenuBar:
  199.                         done = domenubar(MenuSelect(theEvent.where));
  200.                         break;
  201.                     }
  202.                 }
  203.                 
  204.                 break;
  205.                 
  206.             }
  207.         }
  208.     }
  209.  
  210.  
  211.     closemenu();
  212. }
  213.  
  214.  
  215. //****************************************
  216.  
  217. Boolean ACLWorld::docmdkey(EventRecord *event)
  218. {
  219.     char c;
  220.     
  221.     c = event->message & charCodeMask;
  222.     if (event->modifiers & cmdKey)
  223.     {
  224.         return domenubar( MenuKey(c) );
  225.     }
  226.  
  227.     return FALSE;
  228. }
  229.  
  230.  
  231. //****************************************
  232.  
  233. Boolean ACLWorld::domenubar(long menuResult)
  234. {
  235.     short menuID,itemNumber;
  236.     Boolean res = FALSE;
  237.     Str255    str;
  238.     
  239.     menuID = HiWord(menuResult);
  240.     itemNumber = LoWord(menuResult);
  241.  
  242.     if (menuID==2 && itemNumber==1) res = TRUE;
  243.  
  244.     if (menuID==1)
  245.     {
  246.         if (itemNumber==1) about();
  247.         else
  248.         {
  249.             GetItem(menus[0],itemNumber,str);
  250.             OpenDeskAcc(str);
  251.         }
  252.     }
  253.  
  254.     HiliteMenu(0);
  255.     
  256.     return res;
  257. }
  258.  
  259.  
  260. //****************************************
  261.  
  262. static AnimGfx *background;
  263. static short    backwidth,backheight;
  264. static Anim       *anim_pers,*anim_a, *anim_c, *anim_l;
  265.  
  266.  
  267. Boolean    testanimpers(AnimCObject *a, AnimControl *c)    // Used to test if the anim is out of screen.
  268. {
  269.     if (a->getx()>=backwidth) return TRUE;
  270.  
  271.  
  272.     if (anim_pers->checkcollision(anim_a) ||
  273.         anim_pers->checkcollision(anim_c) ||
  274.         anim_pers->checkcollision(anim_l))
  275.     {
  276.         if (anim_pers->getcurrentsequence()!=1) 
  277.         {
  278.             anim_pers->setsequence(1);
  279.             anim_pers->move(0,-20);
  280.         }
  281.     }
  282.     else if (anim_pers->getcurrentsequence()!=0)     
  283.     {
  284.         anim_pers->setsequence(0);
  285.         anim_pers->move(0,20);
  286.     }
  287.  
  288.     return FALSE;
  289. }
  290.  
  291.  
  292. void ACLWorld::initmenu(void)
  293. {
  294.     Anim *anim;
  295.     short w,h,cw,cx,cy,ch;
  296.  
  297.     pleasewait(TRUE);
  298.  
  299.     animbase = new AnimBase();
  300.     
  301.     
  302.     background = animbase->newgfx(PICT_BACKGROUND);
  303.     backwidth = background->getwidth();
  304.     backheight = background->getheight();
  305.         
  306.     animbase->installbackground(background);
  307.     animbase->buildbufferback();
  308.     animbase->setbasex(23);
  309.     animbase->setbasey(8);
  310.     
  311.     animbase->setsortflags(ASORT_PRIORITY);
  312.  
  313.     anim_pers = anim = animbase->createanim(animpers);
  314.  
  315.     anim->findmaxsize(&w,NULL);
  316.     anim->place(-w,18);                // Place animation
  317.     anim->setpriority(-10);
  318.     anim->setcollisionkey_me(2);
  319.     anim->setcollisionkey_hit(1);
  320.     
  321.     
  322.     perscontrols[0].cmd = acmd_move;
  323.     perscontrols[0].x = 2;
  324.     perscontrols[0].y = 0;
  325.     perscontrols[0].next = &perscontrols[1];
  326.         
  327.     perscontrols[1].cmd = acmd_place;
  328.     perscontrols[1].x = -w;
  329.     perscontrols[1].y = 18;
  330.     perscontrols[1].testproc = &testanimpers;
  331.     perscontrols[1].next = &perscontrols[0];
  332.         
  333.     anim->runcontrol(perscontrols);
  334.  
  335.  
  336.     anim_c = anim = animbase->createanim(PICT_C);
  337.     anim->findmaxsize(&cw,&ch);
  338.     cx = (backwidth/2)-(cw/2);
  339.     cy = (backheight/2)-(ch/2);
  340.     anim->place(cx,cy);
  341.     anim->setpriority(0);
  342.     anim->setcollisionkey_me(1);
  343.     anim->setcollisionkey_hit(2);
  344.  
  345.  
  346.     anim_a = anim = animbase->createanim(PICT_A);
  347.     anim->findmaxsize(&w,NULL);
  348.     anim->place(cx-(w+5),cy);
  349.     anim->setpriority(0);
  350.     anim->setcollisionkey_me(1);
  351.     anim->setcollisionkey_hit(2);
  352.     
  353.  
  354.     a_controls[0].cmd = acmd_setpriority;
  355.     a_controls[0].data = -5;
  356.     a_controls[0].next = &a_controls[1];
  357.  
  358.     a_controls[1].cmd = acmd_goto;
  359.     a_controls[1].x = cx+(cw+5);
  360.     a_controls[1].y = cy;
  361.     a_controls[1].speed = 4;
  362.     a_controls[1].acceleration = 0;
  363.     a_controls[1].next = &a_controls[2];
  364.  
  365.     a_controls[2].cmd = acmd_setpriority;
  366.     a_controls[2].data = 5;
  367.     a_controls[2].next = &a_controls[3];
  368.  
  369.     a_controls[3].cmd = acmd_goto;
  370.     a_controls[3].x = cx-(w+5);
  371.     a_controls[3].y = cy;
  372.     a_controls[3].speed = 4;
  373.     a_controls[3].acceleration = 0;
  374.     a_controls[3].next = &a_controls[0];
  375.  
  376.     anim->runcontrol(a_controls);
  377.  
  378.     
  379.     anim_l = anim = animbase->createanim(PICT_L);
  380.     anim->findmaxsize(&w,NULL);
  381.     anim->place(cx+(cw+5),cy);
  382.     anim->setpriority(0);
  383.     anim->setcollisionkey_me(1);
  384.     anim->setcollisionkey_hit(2);
  385.  
  386.  
  387.     l_controls[0].cmd = acmd_setpriority;
  388.     l_controls[0].data = 5;
  389.     l_controls[0].next = &l_controls[1];
  390.  
  391.     l_controls[1].cmd = acmd_goto;
  392.     l_controls[1].x = cx-(w+5);
  393.     l_controls[1].y = cy;
  394.     l_controls[1].speed = 4;
  395.     l_controls[1].acceleration = 0;
  396.     l_controls[1].next = &l_controls[2];
  397.  
  398.     l_controls[2].cmd = acmd_setpriority;
  399.     l_controls[2].data = -5;
  400.     l_controls[2].next = &l_controls[3];
  401.  
  402.     l_controls[3].cmd = acmd_goto;
  403.     l_controls[3].x = cx+(cw+5);
  404.     l_controls[3].y = cy;
  405.     l_controls[3].speed = 4;    
  406.     l_controls[3].acceleration = 0;
  407.     l_controls[3].next = &l_controls[0];
  408.  
  409.     anim->runcontrol(l_controls);
  410.  
  411.  
  412.  
  413.     dialog = GetNewDialog(dlg_FIRSTMENU,NULL,(WindowPtr)-1);
  414.     
  415.     pleasewait(FALSE);
  416.  
  417.  
  418.     ShowWindow(dialog);
  419.     SetPort(dialog);
  420. }
  421.  
  422. //****************************************
  423.  
  424. void ACLWorld::closemenu(void)
  425. {
  426.     delete animbase;
  427.     DisposDialog(dialog);    
  428. }
  429.  
  430. //****************************************
  431.  
  432. void ACLWorld::initmenubar()
  433. {
  434.     short i;
  435.  
  436.     for(i=0;i<3;i++)
  437.     {
  438.         menus[i] = GetMenu(i+1);
  439.         InsertMenu(menus[i],0);
  440.     }
  441.     
  442.     AddResMenu(menus[0],'DRVR');
  443.     
  444.     DrawMenuBar();
  445. }
  446.  
  447. void ACLWorld::freemenubar()
  448. {
  449.     short i;
  450.  
  451.     for(i=0;i<3;i++)
  452.     {
  453.         DeleteMenu(i+1);
  454.         ReleaseResource((Handle)menus[i]);
  455.     }
  456. }
  457.  
  458.  
  459. //****************************************
  460.  
  461. #define sysevtmask (*((short*)0x0144))
  462.  
  463. void main(void)
  464. {
  465.     ACLWorld    *app;
  466.     short        oldmask;
  467.  
  468.     oldmask = sysevtmask;
  469.     sysevtmask |= keyUpMask;        // I want keyUp events
  470.  
  471.     app = new ACLWorld();
  472.     app->run();
  473.     delete app;
  474.  
  475.     sysevtmask = oldmask;    
  476. }
  477.  
  478.